package com.mycompany.session;

import com.mycompany.dao.PomodoroDAO;
import com.mycompany.entity.Pomodoro;
import com.mycompany.interceptor.LoggingInterceptor;
import javax.annotation.Resource;
import javax.annotation.security.DeclareRoles;
import javax.annotation.security.PermitAll;
import javax.annotation.security.RolesAllowed;
import javax.ejb.SessionContext;
import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.interceptor.Interceptors;

@Stateless(name = "PomodoroSessionBean")
@Interceptors(LoggingInterceptor.class)
@DeclareRoles({"user", "leader", "admin"})
public class PomodoroSessionBean {

    @Inject
    PomodoroDAO pomodoroDAO;
    
    @Resource
    SessionContext sctx;

    @RolesAllowed({"user"})
    public Pomodoro persist(Pomodoro t) {
        Pomodoro p = pomodoroDAO.persist(t);
        
        System.out.println("El id generado es: " + p.getId());
        
        if (sctx.isCallerInRole("admin")) {
            System.out.println("Tiene rol administrador");
        }
        
        return p;
    }

    @RolesAllowed({"user"})
    public Pomodoro find(Object id) {
        return pomodoroDAO.find(id);
    }

    @RolesAllowed({"user"})
    public Pomodoro getReference(Pomodoro t) {
        return pomodoroDAO.getReference(t);
    }

    @RolesAllowed({"admin"})
    public void remove(Pomodoro t) {
        pomodoroDAO.remove(t);
    }

    @RolesAllowed({"user"})
    public Pomodoro merge(Pomodoro t) {
        return pomodoroDAO.merge(t);
    }

    @RolesAllowed({"user"})
    public void refresh(Pomodoro t) {
        pomodoroDAO.refresh(t);
    }
}
